<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
        <el-card>
          <!--工具栏-->
          <div class="head-container">
            <div v-if="crud.props.searchToggle">
              <!-- 搜索 -->

              <el-select v-model="query.uniId" class="filter-item" filterable remote reserve-keyword placeholder="请输入大学名称"
                :remote-method="remoteMethod" @change="crud.toQuery()" :loading="select_loading">
                <el-option v-for="item in uniList" :key="item.value" :label="item.uniName" :value="item.uniId">
                </el-option>
              </el-select>
              <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="search">
                搜索
              </el-button>
              <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="add">
                新增
              </el-button>
            </div>
            <crudOperation show="" :permission="permission" />
          </div>
        </el-card>
        <el-card style="margin-top: 20px;">

          <!--表格渲染-->
          <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
            @selection-change="crud.selectionChangeHandler">
            <el-table-column :show-overflow-tooltip="true" prop="enrollmentTypeInfo" label="招生类型" >
              <template slot-scope="scope">
                <span v-if="scope.row.enrollmentType == 1">统考</span>
                <span v-if="scope.row.enrollmentType == 2">校考</span>
              </template>
            </el-table-column>
            <el-table-column prop="batch" label="批次" />
            <el-table-column prop="num" label="数量" />
            <el-table-column label="操作" align="center" width="270">
              <template slot-scope="scope">
                <el-button class="btn" icon="el-icon-edit" type="primary" size="mini" @click="edit(scope.row)" />
                <!-- <el-button class="btn" :loading="scope.row.del_loading" icon="el-icon-delete" type="danger" size="mini" @click="del(scope.row)" /> -->
                <!-- <el-button class="btn" :loading="scope.row.r_loading" icon="el-icon-" type="success" size="mini" @click="changeStatus(scope.row,1,'r_loading')">{{scope.row.ifRecommend ? '不推荐' : '推荐'}}</el-button> -->
                <!-- <el-button class="btn" :loading="scope.row.s_loading" icon="el-icon-" type="warning" size="mini" @click="changeStatus(scope.row,2,'s_loading')">{{scope.row.ifSelected ? '不精选' : '精选'}}</el-button> -->
              </template>
            </el-table-column>
          </el-table>
        </el-card>
        <!--分页组件-->
        <pagination />
    </el-row>
    <addPlanDialog ref="addPlanDialog" @ok="addOk"></addPlanDialog>
  </div>
</template>

<script>
import crudApi from '@/api/web/plan'
import addPlanDialog from './modual/addPlanDialog';

import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import { getList } from '@/api/web/school'
export default {
  name: 'Admin',
  components: {
    crudOperation,
    rrOperation,
    udOperation,
    pagination,
    DateRangePicker,
    addPlanDialog
  },
  cruds() {
    return CRUD({
      title: '用户',
      url: 'api/uni/scheme/list',
      methodStr: 'post',
      dataStr: ['data', 'schemeList'],
      queryOnPresenterCreated: false,
      optShow: {
        reset: false,
      },
      crudMethod: { ...crudApi }
    })
  },
  mixins: [presenter(), header(), crud()],
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      select_loading: false,
      uniList: [],
      defaultProps: {
        children: 'children',
        label: 'name',
        isLeaf: 'leaf'
      },
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'article:edit'],
        del: ['admin', 'user:del']
      }
    }
  },
  mounted: function () {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    add() {
      this.$refs.addPlanDialog.add()
    },
    edit(item) {
      item.uniId = this.query.uniId
      item.uniName = this.uniList.find(v=>v.uniId === this.query.uniId).uniName
      this.$refs.addPlanDialog.edit(item)
    },
    search(){
      if(!this.query.uniId){
        this.$message.error('请选择一所大学')
        return
      }
      this.crud.toQuery()
    },
    del(item) {
      this.$confirm(`文章删除后不可恢复，确认删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$set(item, 'del_loading', true)
        crudApi.del({ forumId: item.forumId }).then(res => {
          this.$set(item, 'del_loading', false)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.msg
            })
            this.crud.toQuery()
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        })
      })
    },
    remoteMethod(query) {
      this.select_loading = true;
      getList({ keyword: query }).then(res => {
        this.select_loading = false
        if (res.code === 200) {
          this.uniList = res.data.uniList
        }
      })
    },
    addOk(uniObj) {
      this.$set(this.query, 'uniId', uniObj.uniId)
      this.crud.toQuery()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}

.n_avatar {
  max-width: 120px;
  max-height: 120px;
}

.btn {
  margin: 0;
}
</style>
